package com.wwwarehouse.financialcenter.fragment.moneywarehouse.bankcard;


import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.tools.DialogTools;
import com.wwwarehouse.common.tools.ToastUtils;
import com.wwwarehouse.financialcenter.R;
import com.wwwarehouse.financialcenter.bean.bindingcard.BindCardBean;
import com.wwwarehouse.financialcenter.constant.FinancialConstant;
import com.wwwarehouse.financialcenter.customview.AutoClickButton;
import com.wwwarehouse.financialcenter.eventbus.RefreshEvent;
import com.wwwarehouse.financialcenter.fragment.base.TheParentFragment;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * Description:
 * Copyright  : Copyright (c) 2017
 * Author     : yfwang
 * Date       : 2017/11/14 13:42
 */
public class UnbindCardInfoFragment extends TheParentFragment {

    private TextView mAccountName;
    private TextView mCardNumber;
    private TextView mBankName;
    private TextView mAccountCity;
    private TextView mBankBranch;
    private AutoClickButton mConfirm;
    private String businessId;
    private String isCardInfomation;
    private BindCardBean.DataBean.ListBean data;

    @Override
    protected int getContentId() {
        return R.layout.financial_center_fragment_card_info;
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof UnbindCardInfoFragment) {
            mActivity.setTitle(getString(R.string.financial_center_jb_card));
        }
    }

    @Override
    protected void init(View view) {
        mAccountName = findView(view, R.id.tv_account_name);
        mCardNumber = findView(view, R.id.tv_card_number);
        mBankName = findView(view, R.id.tv_bank_name);
        mAccountCity = findView(view, R.id.tv_account_city);
        mBankBranch = findView(view, R.id.tv_bank_branch);
        mConfirm = findView(view, R.id.confirm_btn);
        try {
            Bundle bundle = getArguments();
            if (bundle != null) {
                data = (BindCardBean.DataBean.ListBean) bundle.getSerializable("data");
                mAccountName.setText(data.getAccountName());
                mCardNumber.setText(data.getCardNo());
                mBankName.setText(data.getBankName());
                mBankBranch.setText(data.getSubbankName());
                mAccountCity.setText(String.format("%s%s", data.getProvinceName(), data.getCityName()));
                businessId = bundle.getString("businessId");
                isCardInfomation = bundle.getString("isCardInfomation");
            }
        } catch (Exception e) {
        }
        mConfirm.setEnabled(true);
        mConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                DialogTools.getInstance().showCustomDialogPrompt(mActivity, null, getString(R.string.financial_center_comfirm_unbinding_tips), false, new DialogTools.ConfirmListener() {
                    @Override
                    public void setConfirmListener(Dialog dialog, View view, String s) {
                        dialog.dismiss();
                        Map<String, Object> map = new HashMap<>();
                        map.put("businessId", businessId);
                        map.put("bindingUkid", data.getBindingUkid());
                        httpPost(FinancialConstant.UNBINDING_BANKCARD, map, 0, true, "");
                    }
                }, getString(R.string.financial_center_comfirm_binding), getString(R.string.financial_center_consider));

            }
        });

    }

    @Override
    public void onSuccess(CommonClass commonClass, int what) {
        if (what == 0) {
            if (TextUtils.equals("0", commonClass.getCode())) {
                //刷新银行卡列表
                if (!TextUtils.isEmpty(isCardInfomation) && isCardInfomation.equals("true")) {
                    popFragment();
                    popFragment();
                } else {
                    popFragment();
                }
                EventBus.getDefault().post(new RefreshEvent("Refresh"));
            } else {
                ToastUtils.showToastAtTop(commonClass.getMsg());
            }
        }
    }

    @Override
    public void onReLoad(int code) {

    }
}
